#include "Principal.h"

Principal::Principal(QWidget *parent):QWidget(parent)
{
	ui.setupUi(this);

        connect(ui.carregaB, SIGNAL(clicked()), ui.gLWidget1, SLOT(CarregaModel()));



        connect(ui.exTabs, SIGNAL(currentChanged(int)), ui.gLWidget1, SLOT(ChangeRunMode(int)));

        connect(ui.loadImageB, SIGNAL(clicked()), ui.gLWidget1, SLOT(loadImage()));
        connect(ui.gLWidget1, SIGNAL(imageLoaded(QPixmap)), ui.previewL, SLOT(setPixmap(QPixmap)));

        connect(ui.gLWidget1,SIGNAL(objectUpdated(QStringList)),this, SLOT(UpdateObjectList(QStringList)));

        connect(ui.selectObjectB, SIGNAL(currentIndexChanged(QString)),this->ui.gLWidget1, SLOT(SelectObject(QString)));

        connect(ui.texturitzaB,SIGNAL(clicked()),ui.gLWidget1,SLOT(TexturitzarObjecteSeleccionat()));

        connect(ui.horizontalSB, SIGNAL(valueChanged(int)), this->ui.gLWidget1, SLOT(updateSRepeat(int)));
        connect(ui.verticalSB, SIGNAL(valueChanged(int)), this->ui.gLWidget1, SLOT(updateTRepeat(int)));

        connect(ui.xlightSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateLightX(int)));
        connect(ui.ylightSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateLightY(int)));
        connect(ui.zlightSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateLightZ(int)));

        connect(ui.gLWidget1,SIGNAL(objectSelected(int,int)),this,SLOT(UpdateRepeats(int,int)));

        connect(ui.updateProjB,SIGNAL(clicked()),ui.gLWidget1,SLOT(UpdateProjectorPosition()));
        connect(ui.updateTextPorjB,SIGNAL(clicked()),ui.gLWidget1,SLOT(UpdateProjectorTexture()));

        connect( &timer, SIGNAL(timeout()), this, SLOT(UpdateLCDS()));
        timer.start(1000);
}

void Principal::UpdateLCDS() {
    ui.fpsNumber->display(ui.gLWidget1->getFPS());
    ui.VertexNumber->display(ui.gLWidget1->getVertexCount());
}

void Principal::UpdateObjectList(QStringList ids) {
    ui.selectObjectB->clear();
    for (int i = 0; i < ids.size(); i++) {
        ui.selectObjectB->addItem(ids[i]);
    }
    UpdateLight();
}
void Principal::UpdateRepeats(int horizontal, int vertical) {
    ui.horizontalSB->setValue(horizontal);
    ui.verticalSB->setValue(vertical);
}
void Principal::UpdateLightX(int v) {
    ui.xlightSlider->setValue(v);
    UpdateLight();
}
void Principal::UpdateLightY(int v) {
    ui.ylightSlider->setValue(v);
    UpdateLight();
}
void Principal::UpdateLightZ(int v) {
    ui.zlightSlider->setValue(v);
    UpdateLight();
}
void Principal::UpdateLight() {
    ui.gLWidget1->UpdateLight(ui.xlightSlider->value(), ui.ylightSlider->value(), ui.zlightSlider->value());
}
